<?php include "../public/header.php";?>
<?php include "../public/header_edit.php";?>
<body id="optPage">
<form class="layui-form layui-form-pane" id="commentForm">
    <div class="page-title"><div class="page-title-con"><span id="flowsetname"></span><div class="move_title"></div><?php include "../public/subturn.php";?></div></div>
    <div class="main-container">
        <div class="block-generic">
            <?php include "../public/substt.php";?>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="edittits"><span>项目信息</span></div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label"><text>*</text>项目名称</label>
                        <div class="layui-input-block">
                            <input type="text" readonly name="enginid" class="layui-input enginid" onclick="openPage('../public/choose_engin.php?dom=enginid&page=labour_yong','80%','80%')">
                            <div class="choose_div" id="enginid" key="1"></div>
                            <span class="choose_one" onclick="openPage('../public/choose_engin.php?dom=enginid&page=labour_yong','80%','80%')"><i class="layui-icon layui-icon-search"></i></span>
                        </div>
                    </div>
                </div>
            </div>


            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="edittits"><span>用工信息</span></div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>用工单单号</label>
                        <div class="layui-input-block">
                            <input type="text" id="number" placeholder="请输入" disabled autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>用工主题</label>
                        <div class="layui-input-block">
                            <input type="text" name="title" id="title" placeholder="请输入" clearable autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>用工性质</label>
                        <div class="layui-input-block">
                            <select id="yongxing" name="yongxing" lay-filter="yongxing" ></select>
                            <p class="error" id="yongxing_error" style="margin: 0;"></p>
                        </div>
                    </div>
                    <div class="layui-col-md6 layui-hide" id="caiiddiv">
                        <label class="layui-form-label"><text>*</text>关联合同</label>
                        <div class="layui-input-block">
                            <input type="text" readonly name="caiid" class="layui-input caiid" onclick="choose_contract_cai()">
                            <div class="choose_div" id="caiid" key="1"></div>
                            <span class="choose_one" onclick="choose_contract_cai()"><i class="layui-icon layui-icon-search"></i></span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">用工时间</label>
                        <div class="layui-input-block">
                            <input type="text" id="yongdt" name="yongdt" placeholder="请选择"  autocomplete="off" class="layui-input bdate_sea_range_ymd">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>登记金额(元)</label>
                        <div class="layui-input-block">
                            <input type="text" name="money" id="money" placeholder="自动计算(根据明细)" disabled autocomplete="off" class="layui-input moneyinput disabled">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>填报人</label>
                        <div class="layui-input-block">
                            <input type="text" name="baner" id="baner" placeholder="请输入" clearable autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>填报日期</label>
                        <div class="layui-input-block">
                            <input type="text" name="dt" id="dt" placeholder="请选择" clearable  autocomplete="off" class="layui-input bdate_ymd">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item height_auto">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label label_textarea">备注</label>
                        <div class="layui-input-block">
                            <textarea id="explain" name="explain" placeholder="请输入" class="layui-textarea"></textarea>
                        </div>
                    </div>
                </div>
            </div>

            <div class="layui-form-item height_auto">
                <div class="short_table_list" key="1">
                    <div class="opt-btn-group">
                        <div><span>用工明细</span></div>
                        <div>
                            <button type="button" onclick="choose_labour_xiang()" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn">从项目花名册导入</button>
                            <button type="button" onclick="ctrlv_data_open()" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn">粘贴新增</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_copy_row">复制行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_add_row">插入行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_row">删除行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_all">删除全部</button>
                        </div>
                    </div>
                    <table class="layui-table" lay-size="sm">
                        <thead>
                        <tr>
                            <th width="45">序号</th>
                            <th><text>*</text>工人姓名</th>
                            <th><text>*</text>身份证号</th>
                            <th>联系电话</th>
                            <th>工种</th>
                            <th><text>*</text>工资类型</th>
                            <th><text>*</text>工价(元)</th>
                            <th><text>*</text>工数</th>
                            <th>工资(元)</th>
                            <th>开户行</th>
                            <th>银行卡号</th>
                            <th>班组名称</th>
                            <th>备注</th>
                        </tr>
                        </thead>
                        <tbody class="short_table_tbody" id="list_list"></tbody>
                    </table>
                    <div class="listerror">提示：<span></span></div>
                </div>
            </div>

            <?php include '../public/flowqita.php'?>
            <?php include '../public/upinfo.php'?>
        </div>
    </div>
</form>
<script>
    var param_id = getQueryVariable('id') || 0;
    chu_flow_edit_yushen();

    $("#commentForm").validate({
        rules: {
            title: {required: true},
            baner: {required: true},
            dt: {required: true},
        },
        messages:{
            title: {required: "请输入"},
            baner: {required: "请输入"},
            dt: {required: "请选择"},
        }
    });

    function choose_contract_cai(obj){
        var enginid = get_choose_val('enginid');
        if(!enginid){
            facade.error("请先选择项目名称");return false;
        }
        openPage('../public/choose_contract_cai.php?dom=caiid&enginid='+enginid+'&page=labour_yong&caitype=ren','80%','80%')
    }

    function choose_labour_xiang(){
        var enginid = get_choose_val('enginid');
        if(!enginid){
            facade.error("请先选择项目名称");return false;
        }
        openPage('../public/choose_labour_xiang.php?&page=labour_yong&choose_type=checkbox&enginid='+enginid,'80%','80%')
    }

    var pub_gongtype, pub_gonglei;
    layui.use(['laytp', 'form'], function() {
        var form = layui.form, layer = layui.layer;
        render_input_attr();

        facade.ajax({type: 'get', url: ask_flowdata_edit_labour_yong, data:{id:param_id}}).done(function(res){
            if (res.code === 2000) {
                var data = res.data.data;
                chu_flow_qian(data);

                var engininfo = res.data.engininfo;
                if(engininfo){
                    set_choose_btn('enginid',engininfo.id,engininfo.name);
                }



                $("#title").val(data.title);
                $("#number").val(data.number);
                $("#yongdt").val(data.yongdt);
                $("#money").val(data.money);
                $("#baner").val(data.baner);
                $("#dt").val(data.dt);
                $("#explain").val(data.explain);

                xiala_zhan_one_you('yongxing', res.data.yongxing, data.yongxing);


                if(data.yongxing == '劳务分包'){
                    var caiidinfo = res.data.caiidinfo;
                    if(caiidinfo){
                        set_choose_btn('caiid',caiidinfo.id,caiidinfo.name);
                    }
                    $("#caiiddiv").removeClass('layui-hide');
                }


                pub_gongtype = res.data.gongtype;
                pub_gonglei = res.data.gonglei;

                var listAddStr = '';
                listAddStr += '<tr>';
                listAddStr += '<td style="width: 45px">1</td>';
                listAddStr += '<td><input type="text" class="layui-input name"></td>';
                listAddStr += '<td><input type="text" class="layui-input idcard"></td>';
                listAddStr += '<td><input type="text" class="layui-input tel"></td>';
                listAddStr += '<td>';
                listAddStr += '<div class="gongtype"></div>';
                listAddStr += '</td>';
                listAddStr += '<td>';
                listAddStr += '<div class="gonglei"></div>';
                listAddStr += '</td>';
                listAddStr += '<td><input type="number" class="layui-input gongfee"></td>';
                listAddStr += '<td><input type="number" class="layui-input gongnum"></td>';
                listAddStr += '<td><input type="number" class="layui-input money disabled" readonly></td>';
                listAddStr += '<td><input type="text" class="layui-input bankname"></td>';
                listAddStr += '<td><input type="text" class="layui-input banknum"></td>';
                listAddStr += '<td><input type="text" class="layui-input banzu"></td>';
                listAddStr += '<td><input type="text" class="layui-input explain"></td>';
                listAddStr += '</tr>';
                short_table_opt_func_duo(listAddStr, 1, [{name:'gongtype', data:res.data.gongtype, value:''},{name:'gonglei', data:res.data.gonglei, value:''}]);


                var list = res.data.list;
                if(list.length > 0){
                    for (var key in list){
                        var str = '';
                        var nums = parseInt(key)+1;
                        str += '<tr>';
                        str += '<td style="width: 45px">'+nums+'</td>';
                        str += '<td><input type="text" class="layui-input name" value="'+list[key].name+'"></td>';
                        str += '<td><input type="text" class="layui-input idcard" value="'+list[key].idcard+'"></td>';
                        str += '<td><input type="text" class="layui-input tel" value="'+list[key].tel+'"></td>';
                        str += '<td><div class="gongtype gongtype'+nums+'" key="'+nums+'"></div></td>';
                        str += '<td><div class="gonglei gonglei'+nums+'" key="'+nums+'"></div></td>';
                        str += '<td><input type="number" class="layui-input gongfee" value="'+list[key].gongfee+'"></td>';
                        str += '<td><input type="number" class="layui-input gongnum" value="'+list[key].gongnum+'"></td>';
                        str += '<td><input type="number" class="layui-input money disabled" readonly  value="'+list[key].money+'"></td>';
                        str += '<td><input type="text" class="layui-input bankname" value="'+list[key].bankname+'"></td>';
                        str += '<td><input type="text" class="layui-input banknum" value="'+list[key].banknum+'"></td>';
                        str += '<td><input type="text" class="layui-input banzu" value="'+list[key].banzu+'"></td>';
                        str += '<td><input type="text" class="layui-input explain" value="'+list[key].explain+'"></td>';
                        str += '</tr>';
                        $("#list_list").html(str);
                        xmselect_list_dan('gongtype'+nums, res.data.gongtype, list[key].gongtype);
                        xmselect_list_dan('gonglei'+nums, res.data.gonglei, list[key].gonglei);
                    }
                }
                update_refresh_row_func();


                public_upload_file_load('upload_files', res.data.files)
                render_from();
                initParseMoney();
            }
        });

        form.on('select(yongxing)', function(data){
            if(data.value == '劳务分包'){
                $("#caiiddiv").removeClass('layui-hide');
            }else{
                $("#caiiddiv").addClass('layui-hide');
            }
            if(data.value == ''){
                $('#yongxing_error').show().text('请选择');
                $('#yongxing_error').parent().find('input').addClass('select_error');
            }else{
                $('#yongxing_error').hide();
                $('#yongxing_error').parent().find('input').removeClass('select_error');
            }
        });

        form.on('submit(add)', function(data){
            data.field.isturn = $(this).attr('key') || 0;
            data.field.isshen = $(this).attr('isshen') || 0;
            data.field.flowrelas = get_choose_val('flowrelas');
            data.field.advaners = tan_tags_getvalue("#advaners", 'user');
            data.field.nexter = tan_tags_getvalue("#nexter", 'user');


            data.field.enginid = get_choose_val('enginid');
            data.field.caiid = get_choose_val('caiid');
            if(data.field.isturn > 0){
                var validRes = true;

                formValid('enginid');
                if(data.field.enginid == undefined || !data.field.enginid){
                    validRes = false;
                }


                if(data.field.yongxing == '劳务分包'){
                    formValid('caiid');
                    if(data.field.caiid == undefined || !data.field.caiid){
                        validRes = false;
                    }
                }

                if(data.field.yongxing == ''){
                    $('#yongxing_error').show().text('请选择');
                    $('#yongxing_error').parent().find('input').addClass('select_error');
                    validRes = false;
                }else{
                    $('#yongxing_error').hide();
                    $('#yongxing_error').parent().find('input').removeClass('select_error');
                }

                if(!validRes){return false;}
            }



            var list = [''];
            try {
                $('#list_list tr').each(function (i) {
                    var name = $(this).find('.name').val();
                    var idcard = $(this).find('.idcard').val();
                    var tel = $(this).find('.tel').val();
                    var gongtype = $(this).find('.gongtype').find('input').val();
                    var gonglei = $(this).find('.gonglei').find('input').val();
                    var gongfee = $(this).find('.gongfee').val();
                    var gongnum = $(this).find('.gongnum').val();
                    var money = $(this).find('.money').val();
                    var bankname = $(this).find('.bankname').val();
                    var banknum = $(this).find('.banknum').val();
                    var banzu = $(this).find('.banzu').val();
                    var explain = $(this).find('.explain').val();

                    var index = i + 1;
                    if(data.field.isturn > 0){
                        if(!name){
                            throw new Error('第'+index+'行，请输入工人姓名');
                        }
                        if(!idcard){
                            throw new Error('第'+index+'行，请输入身份证号');
                        }
                        if(!gonglei){
                            throw new Error('第'+index+'行，请输入工资类型');
                        }
                        if(!gongfee){
                            throw new Error('第'+index+'行，请输入工价');
                        }
                        if(!gongnum){
                            throw new Error('第'+index+'行，请输入工数');
                        }
                    }


                    list.push({
                        name:name,
                        idcard:idcard,
                        tel:tel,
                        gongtype:gongtype,
                        gonglei:gonglei,
                        gongfee:gongfee,
                        gongnum:gongnum,
                        money:money,
                        bankname:bankname,
                        banknum:banknum,
                        banzu:banzu,
                        explain:explain,
                    });
                });
            } catch(e) {
                $(".listerror").find('span').text(e.message);
                $(".listerror").show();
                return false;
            }
            $(".listerror").hide();
            data.field.list = list;


            data.field.id = param_id;
            data.field.files = public_upload_file_value('upload_files');
            data = parseMoney(data);

            public_flow_submit(data.field, ask_flowdata_add_labour_yong, ask_flowdata_edit_labour_yong, $(this).attr('but'));
            return false;
        });

    });



    $('#list_list').on('input propertychange','.gongfee,.gongnum',function () {
        jisuan_money(this);
    });
    function jisuan_money(obj) {
        var gongfee = parseValidNum($(obj).parents('tr').find('.gongfee').val());
        var gongnum = parseValidNum($(obj).parents('tr').find('.gongnum').val());
        var money = (gongfee * gongnum).toFixed(2);
        $(obj).parents('tr').find('.money').val(money);

        jisuan_money_all();
    }


    function jisuan_money_all() {
        var allmoney = 0;
        $('#list_list tr').each(function (i) {
            var money = parseValidNum($(this).find('.money').val())
            allmoney = parseFloat(allmoney) + parseFloat(money);
        });
        $("#money").val(allmoney);
        initParseMoney();
    }


    $('#list_list').on('input propertychange','.money',function () {
        jisuan_money_all();
    });



    function update_refresh_row_func() {
        render_date();
        render_from();
        jisuan_money_all();
        $.each($("#list_list").children('tr'),function (key,val) {
            $(this).children('td:first-child').html(key+1);
        });
        setTimeout(function () {
            $(".short_table_list table tbody tr").click(function () {
                $(".short_table_list table tbody tr").removeClass('line-row');
                $(".short_table_list table tbody tr").attr('sle','');
                $(this).addClass('line-row');
                $(this).attr('sle','sle');
            });
        },1000);
    }






    var ctrlv_field_name = [
        {field: 'name', name:'工人姓名'},
        {field: 'idcard', name:'身份证号'},
        {field: 'tel', name:'联系电话'},
        {field: 'gongtype', name:'工种'},
        {field: 'gonglei', name:'工资类型'},
        {field: 'gongfee', name:'工价',type:'number'},
        {field: 'gongnum', name:'工数',type:'number'},
        {field: 'bankname', name:'开户行'},
        {field: 'banknum', name:'银行卡号'},
        {field: 'banzu', name:'班组名称'},
        {field: 'explain', name:'备注'},
    ];
    function ctrlv_data_show(list) {
        if(list.length > 0){
            var nums = $("#list_list tr").length;
            for (var key in list){
                var str = '';
                nums++;
                str += '<tr>';
                str += '<td style="width: 45px">'+nums+'</td>';
                str += '<td><input type="text" class="layui-input name" value="'+list[key].name+'"></td>';
                str += '<td><input type="text" class="layui-input idcard" value="'+list[key].idcard+'"></td>';
                str += '<td><input type="text" class="layui-input tel" value="'+list[key].tel+'"></td>';
                str += '<td><div class="gongtype gongtype'+nums+'" key="'+nums+'"></div></td>';
                str += '<td><div class="gonglei gonglei'+nums+'" key="'+nums+'"></div></td>';
                str += '<td><input type="number" class="layui-input gongfee" value="'+list[key].gongfee+'"></td>';
                str += '<td><input type="number" class="layui-input gongnum" value="'+list[key].gongnum+'"></td>';
                str += '<td><input type="number" class="layui-input money disabled" readonly ></td>';
                str += '<td><input type="text" class="layui-input bankname" value="'+list[key].bankname+'"></td>';
                str += '<td><input type="text" class="layui-input banknum" value="'+list[key].banknum+'"></td>';
                str += '<td><input type="text" class="layui-input banzu" value="'+list[key].banzu+'"></td>';
                str += '<td><input type="text" class="layui-input explain" value="'+list[key].explain+'"></td>';
                str += '</tr>';

                $("#list_list").append(str);
                xmselect_list_dan('gongtype'+nums, pub_gongtype, ctrlvGetJiShuValue(pub_gongtype, list[key].gongtype));
                xmselect_list_dan('gonglei'+nums, pub_gonglei, ctrlvGetJiShuValue(pub_gonglei, list[key].gonglei));
            }
        }
        update_refresh_row_func();
        render_date();
        render_from();
    }



</script>
</body>
</html>
